using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.ServiceObjects;

namespace OracleInternal.ConnectionPool
{
	internal class OraclePool : Pool<OraclePoolManager, OraclePool, OracleConnectionImpl>
	{
		public bool m_bIsPoolActive;

		public static bool m_bPerfCounterEnabled;

		public static bool m_bPerfHardConnectsPerSecond;

		public static bool m_bPerfHardDisconnectsPerSecond;

		public static bool m_bPerfSoftConnectsPerSecond;

		public static bool m_bPerfSoftDisconnectsPerSecond;

		public static bool m_bPerfNumberOfActiveConnectionPools;

		public static bool m_bPerfNumberOfInactiveConnectionPools;

		public static bool m_bPerfNumberOfActiveConnections;

		public static bool m_bPerfNumberOfFreeConnections;

		public static bool m_bPerfNumberOfPooledConnections;

		public static bool m_bPerfNumberOfNonPooledConnections;

		public static bool m_bPerfNumberOfReclaimedConnections;

		public static bool m_bPerfNumberOfStasisConnections;

		private OraclePerfCounter[] m_perfCounterCache = new OraclePerfCounter[12];

		static OraclePool()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2304, OracleTraceClassName.OraclePool, OracleTraceFuncName.cctor);
			}
			try
			{
				ushort num = (ushort)ConfigBaseClass.m_PerformanceCounters;
				m_bPerfCounterEnabled = num > 0;
				m_bPerfHardConnectsPerSecond = (num & 1) != 0;
				m_bPerfHardDisconnectsPerSecond = (num & 2) != 0;
				m_bPerfSoftConnectsPerSecond = (num & 4) != 0;
				m_bPerfSoftDisconnectsPerSecond = (num & 8) != 0;
				m_bPerfNumberOfActiveConnectionPools = (num & 0x10) != 0;
				m_bPerfNumberOfInactiveConnectionPools = (num & 0x20) != 0;
				m_bPerfNumberOfActiveConnections = (num & 0x40) != 0;
				m_bPerfNumberOfFreeConnections = (num & 0x80) != 0;
				m_bPerfNumberOfPooledConnections = (num & 0x100) != 0;
				m_bPerfNumberOfNonPooledConnections = (num & 0x200) != 0;
				m_bPerfNumberOfReclaimedConnections = (num & 0x400) != 0;
				m_bPerfNumberOfStasisConnections = (num & 0x800) != 0;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268437504, OracleTraceClassName.OraclePool, OracleTraceFuncName.cctor, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2560, OracleTraceClassName.OraclePool, OracleTraceFuncName.cctor);
				}
			}
		}

		public static void PerformanceCounterDecrement(OraclePerfParams.CounterIndex counterIndex, OracleConnectionImpl pr, OraclePool op)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2304, OracleTraceClassName.OraclePool, OracleTraceFuncName.PerformanceCounterDecrement);
			}
			try
			{
				if (op == null)
				{
					OraclePerfCounterCollection.Decrement(counterIndex, pr.m_cs.m_poolName, GetInstanceName(pr));
				}
				else
				{
					op.m_perfCounterCache[(int)counterIndex].Decrement();
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268437504, OracleTraceClassName.OraclePool, OracleTraceFuncName.PerformanceCounterDecrement, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2560, OracleTraceClassName.OraclePool, OracleTraceFuncName.PerformanceCounterDecrement);
				}
			}
		}

		public static void PerformanceCounterIncrement(OraclePerfParams.CounterIndex counterIndex, OracleConnectionImpl pr, OraclePool op)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2304, OracleTraceClassName.OraclePool, OracleTraceFuncName.PerformanceCounterIncrement);
			}
			try
			{
				OraclePerfCounter oraclePerfCounter = null;
				if (op == null || op.m_perfCounterCache[(int)counterIndex] == null)
				{
					oraclePerfCounter = OraclePerfCounterCollection.Increment(counterIndex, pr.m_cs.m_poolName, GetInstanceName(pr));
					if (op != null)
					{
						op.m_perfCounterCache[(int)counterIndex] = oraclePerfCounter;
					}
				}
				else
				{
					op.m_perfCounterCache[(int)counterIndex].Increment();
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268437504, OracleTraceClassName.OraclePool, OracleTraceFuncName.PerformanceCounterIncrement, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2560, OracleTraceClassName.OraclePool, OracleTraceFuncName.PerformanceCounterIncrement);
				}
			}
		}

		private static string GetInstanceName(OracleConnectionImpl pr)
		{
			string result = null;
			if (pr.m_cs.m_haEvents || pr.m_cs.m_loadBalancing)
			{
				result = pr.m_instanceName;
			}
			return result;
		}

		public override void PutNewPR(OracleConnectionImpl pr, bool bForPoolPopulation)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2304, OracleTraceClassName.OraclePool, OracleTraceFuncName.PutNewPR);
			}
			try
			{
				base.PutNewPR(pr, bForPoolPopulation);
				if (!m_bPerfCounterEnabled)
				{
					return;
				}
				if (bForPoolPopulation && m_bPerfNumberOfFreeConnections)
				{
					PerformanceCounterIncrement(OraclePerfParams.CounterIndex.NumberOfFreeConnections, pr, this);
				}
				if (m_bPerfNumberOfPooledConnections)
				{
					PerformanceCounterIncrement(OraclePerfParams.CounterIndex.NumberOfPooledConnections, pr, this);
				}
				if (!m_bIsPoolActive)
				{
					if (m_bPerfNumberOfActiveConnectionPools)
					{
						PerformanceCounterIncrement(OraclePerfParams.CounterIndex.NumberOfActiveConnectionPools, pr, this);
					}
					if (m_bPerfNumberOfInactiveConnectionPools)
					{
						PerformanceCounterDecrement(OraclePerfParams.CounterIndex.NumberOfInactiveConnectionPools, pr, this);
					}
					m_bIsPoolActive = true;
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268437504, OracleTraceClassName.OraclePool, OracleTraceFuncName.PutNewPR, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2560, OracleTraceClassName.OraclePool, OracleTraceFuncName.PutNewPR);
				}
			}
		}

		public override OracleConnectionImpl Get(OracleConnection connRefForCriteria)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2304, OracleTraceClassName.OraclePool, OracleTraceFuncName.Get);
			}
			try
			{
				OracleConnectionImpl oracleConnectionImpl = base.Get(connRefForCriteria);
				if (m_bPerfCounterEnabled && oracleConnectionImpl != null && m_bPerfNumberOfFreeConnections)
				{
					PerformanceCounterDecrement(OraclePerfParams.CounterIndex.NumberOfFreeConnections, oracleConnectionImpl, this);
				}
				return oracleConnectionImpl;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268437504, OracleTraceClassName.OraclePool, OracleTraceFuncName.Get, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2560, OracleTraceClassName.OraclePool, OracleTraceFuncName.Get);
				}
			}
		}
	}
}
